package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.entity.Psych;
import com.lanou.entity.PsychReply;
import com.lanou.service.PsychService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("psych")
public class PsychController {
    @Autowired
    private PsychService psychService;
    @GetMapping("byPage")
    public R getAllPsych(
                         @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize) {
        PageInfo<Psych> psychPageInfo = psychService.selectAllPsych(pageNum, pageSize);
        return R.ok(psychPageInfo);
    }
    @GetMapping("{id}")
    public R getPsychById(@PathVariable Integer id) {
        Psych psych = psychService.selectPsychByIdy(id);
        return R.ok(psych);
    }
    @PostMapping
    public R addPsychReply(@RequestBody Psych psych) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime1 = now.format(formatter1);
        psych.setCreateTime(formattedTime1);
        System.out.println(psych+"----------");
        int i = psychService.insertPsych(psych);
        return R.ok(i);
    }

}
